import pandas as pd
import numpy as np
from sklearn.metrics import accuracy_score, mean_squared_error, r2_score, mean_squared_log_error, mean_absolute_error
from sklearn.preprocessing import StandardScaler,MinMaxScaler
from scipy.spatial import distance
from sklearn.tree import DecisionTreeRegressor
from sklearn.linear_model import LinearRegression, SGDRegressor
from sklearn.ensemble import RandomForestRegressor, GradientBoostingRegressor, AdaBoostRegressor, BaggingRegressor, ExtraTreesRegressor
import sklearn.model_selection as ms
from sklearn.neighbors import KNeighborsRegressor
import xgboost as xgb
from sklearn.linear_model import Lasso, Ridge, ElasticNet
import datetime
from SKO.AbstractDPJob import AbstractDPJob
from Predict_FESJob import Predict_FESJob
class Predict_FES_peimeiJob(AbstractDPJob):
    def __init__(self,
                 p_date=None,p_bf_no=None,p_avg_iron_temp=None,p_avg_c_s_value=None,p_compute_slag_rate=None,p_compute_fill_s_value=None,p_zongjiaobi=None):
        super(Predict_FES_peimeiJob, self).__init__()
        self.date = p_date
        self.bf_no = p_bf_no
        self.avg_iron_temp = p_avg_iron_temp
        self.avg_c_s_value = p_avg_c_s_value
        self.compute_slag_rate = p_compute_slag_rate
        self.compute_fill_s_value = p_compute_fill_s_value
        # self.penchuimei_s = p_penchuimei_s
        # self.meibi = p_meibi
        self.zongjiaobi = p_zongjiaobi

        pass


    def execute(self):
        return self.do_execute()


    def do_execute(self):
        super(Predict_FES_peimeiJob, self).do_execute()
        #预测铁水硫接口传入参数
        date = self.date
        bf_no = self.bf_no
        avg_iron_temp = self.avg_iron_temp
        avg_c_s_value = self.avg_c_s_value
        compute_slag_rate = self.compute_slag_rate
        compute_fill_s_value = self.compute_fill_s_value
        # penchuimei_s = self.penchuimei_s
        # meibi = self.meibi
        zongjiaobi = self.zongjiaobi
        # start = datetime.datetime.now()
        # 煤种使用数量及价格
        xlsx_name = 'D:/repos/sicost/COKE2使用数量.xlsx'
        df5 = pd.read_excel(xlsx_name)
        df5.columns = df5.columns.str.upper()
        df5.WT.fillna(0, inplace=True)
        df5.AD_WT.fillna(0, inplace=True)
        df5_new = df5[(df5['WT'] > 0) | (df5['AD_WT'] > 0)]
        df5_new = df5_new.reset_index(drop=True)

        # 煤质
        xlsx_name = 'D:/repos/sicost/COKE2煤质.xlsx'
        df6 = pd.read_excel(xlsx_name)
        df6.columns = df6.columns.str.upper()
        df6.drop(['VAR'], axis=1, inplace=True)
        df6.drop(['CLASS'], axis=1, inplace=True)
        df6.drop(['SOURCE'], axis=1, inplace=True)
        df6.drop(['PROD_DSCR'], axis=1, inplace=True)
        df6.drop(['H2O'], axis=1, inplace=True)
        df6.drop(['ASH'], axis=1, inplace=True)
        df6.drop(['COKING_COALBLD_BOND_IND'], axis=1, inplace=True)
        df6.drop(['M_L3_JZCHD_Y'], axis=1, inplace=True)
        df6.drop(['COKING_COALBLD_GIESFLU'], axis=1, inplace=True)
        df6.drop(['L_M_AB'], axis=1, inplace=True)
        df6.drop(['C'], axis=1, inplace=True)
        df6.drop(['COKE_HOTVALUE'], axis=1, inplace=True)
        v = ['PROD_CODE']
        df_56 = pd.merge(df5_new, df6, on=v, how='left')
        df_56['TOTAL_COKE_VM'] = df_56['COKE_VM'] * df_56['WT']
        df_56['TOTAL_S'] = df_56['S'] * df_56['WT']
        df_56['TOTAL_PRICE'] = df_56['PRICE'] * df_56['WT']
        df_56['AD_TOTAL_COKE_VM'] = df_56['COKE_VM'] * df_56['AD_WT']
        df_56['AD_TOTAL_S'] = df_56['S'] * df_56['AD_WT']
        df_56['AD_TOTAL_PRICE'] = df_56['PRICE'] * df_56['AD_WT']

        total_wt = df_56['WT'].sum()
        ad_total_wt = df_56['AD_WT'].sum()
        total_s = df_56['TOTAL_S'].sum()
        ad_total_s = df_56['AD_TOTAL_S'].sum()
        total_vm = df_56['TOTAL_COKE_VM'].sum()
        ad_total_vm = df_56['AD_TOTAL_COKE_VM'].sum()
        total_price = df_56['TOTAL_PRICE'].sum()
        ad_total_price = df_56['AD_TOTAL_PRICE'].sum()
        old_unit_price = total_price / total_wt
        new_unit_price = ad_total_price / ad_total_wt
        old_s = total_s / total_wt
        new_s = ad_total_s / ad_total_wt
        old_vm = total_vm / total_wt
        new_vm = ad_total_vm / ad_total_wt
        data_jiaotancanshu = pd.read_excel('COKE2参数.xlsx')
        data_jiaotancanshu.columns = data_jiaotancanshu.columns.str.upper()
        parm_4 = data_jiaotancanshu[(data_jiaotancanshu['PARM_CHN'] == 'parm_4')]
        parm_4 = parm_4['PARM_CALC'].values[0]
        parm_5 = data_jiaotancanshu[(data_jiaotancanshu['PARM_CHN'] == 'parm_5')]
        parm_5 = parm_5['PARM_CALC'].values[0]
        parm_6 = data_jiaotancanshu[(data_jiaotancanshu['PARM_CHN'] == 'parm_6')]
        parm_6 = parm_6['PARM_CALC'].values[0]
        old_jiaotan_s = parm_4 + parm_5 * (old_s) / (100 - old_vm) - parm_6 * (old_vm)
        new_jiaotan_s = parm_4 + parm_5 * (new_s) / (100 - new_vm) - parm_6 * (new_vm)


        delta_unit_price = new_unit_price - old_unit_price
        delta_jiaotan_s = new_jiaotan_s - old_jiaotan_s
        delta_dairuliu = delta_jiaotan_s * zongjiaobi

        old_dairuliu = compute_fill_s_value
        new_dairuliu = old_dairuliu + delta_dairuliu
        y_pred_output = Predict_FESJob(p_bf_no=bf_no, p_avg_iron_temp=avg_iron_temp, p_avg_c_s_value=avg_c_s_value,
                                       p_compute_slag_rate=compute_slag_rate,
                                       p_compute_fill_s_value=new_dairuliu).execute()

        print('finish')
        return y_pred_output,delta_unit_price


